﻿namespace Yipusoft.BaseDoc.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Runtime.InteropServices;
    using Yipusoft.BaseDoc.DAL;
    using Yipusoft.BaseDoc.Model;

    public class FileManager
    {
        private static readonly Yipusoft.BaseDoc.DAL.FileManager dal = new Yipusoft.BaseDoc.DAL.FileManager();

        public static bool AddFile(DocumentFileInfo file)
        {
            return dal.AddFile(file);
        }

        public static bool DeleteFile(Guid ID)
        {
            return dal.DeleteFile(ID);
        }

        public static int GetChildFileCount(Guid ID, bool Flag)
        {
            return dal.GetChildFileCount(ID, Flag);
        }

        public static DocumentFileInfo GetFile(Guid ID)
        {
            return dal.GetFile(ID);
        }

        public static int GetFileCountByParent(DocumentFileInfo info)
        {
            int num = 0;
            string where = " ParentID='" + info.ID + "'";
            List<DocumentFileInfo> list = dal.GetList(where);
            for (int i = 0; i < list.Count; i++)
            {
                DocumentFileInfo info2 = list[i];
                if (list[i].IsFolder)
                {
                    num += GetFileCountByParent(info2);
                }
                else
                {
                    num++;
                }
            }
            return num;
        }

        public static int GetFileSizeByParent(DocumentFileInfo info)
        {
            int num = 0;
            string where = " ParentID='" + info.ID + "'";
            List<DocumentFileInfo> list = dal.GetList(where);
            for (int i = 0; i < list.Count; i++)
            {
                DocumentFileInfo info2 = list[i];
                if (list[i].IsFolder)
                {
                    num += GetFileSizeByParent(info2);
                }
                else
                {
                    num += list[i].FileSize.Value;
                }
            }
            return num;
        }

        public static List<DocumentFileInfo> GetList(string where)
        {
            return dal.GetList(where);
        }

        public static List<DocumentFileInfo> GetList(string where, int pageIndex, int pageSize, out int Count)
        {
            return dal.GetList(where, pageIndex, pageSize, out Count);
        }

        public static DataTable GetMonthFileCount(string Where)
        {
            return dal.GetMonthFileCount(Where);
        }

        public static int GetMonthFileCount(string departmentid, int year, int month)
        {
            int num = 0;
            num = GetTempFileCount(departmentid, year, month, false);
            string where = string.Concat(new object[] { " Departmentid='", departmentid, "' and ParentID='00000000-0000-0000-0000-000000000000' and year(StartDate)=", year, " and Month(StartDate)=", month, " and isFolder=1" });
            List<DocumentFileInfo> list = dal.GetList(where);
            for (int i = 0; i < list.Count; i++)
            {
                DocumentFileInfo info = list[i];
                num += GetFileCountByParent(info);
            }
            return num;
        }

        public static int GetMonthFileSize(string departmentid, int year, int month)
        {
            int num = 0;
            num = GetTempFileSize(departmentid, year, month);
            string where = string.Concat(new object[] { " Departmentid='", departmentid, "' and ParentID='00000000-0000-0000-0000-000000000000' and year(StartDate)=", year, " and Month(StartDate)=", month, " and isFolder=1" });
            List<DocumentFileInfo> list = dal.GetList(where);
            for (int i = 0; i < list.Count; i++)
            {
                DocumentFileInfo info = list[i];
                num += GetFileSizeByParent(info);
            }
            return num;
        }

        public static int GetTempFileCount(string departmentid, int year, int month, bool Flag)
        {
            return dal.GetTempFileCount(departmentid, year, month, Flag);
        }

        public static int GetTempFileSize(string departmentid, int year, int month)
        {
            return dal.GetTempFileSize(departmentid, year, month);
        }

        public static bool UpdateDocumentFileInfo(DocumentFileInfo file)
        {
            return dal.UpdateDocumentFileInfo(file);
        }

        public static bool UpdateFileSize(Guid ID)
        {
            return dal.UpdateFileSize(ID);
        }
    }
}

